package com.yanqu.road.dao.impl.monopolymarket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.group.MonopolyMarketCrossGroup;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/5 18:50
 */
public class MonopolyMarketCrossGroupDaoImpl extends TempDataObjectDao<MonopolyMarketCrossGroup> {
    public static String getMonopolyMarketCrossGroupSql = "select * from `t_c_monopolymarket_cross_group` where `channelId`=? ";
    public Map<Long,MonopolyMarketCrossGroup> getMonopolyMarketCrossGroupMap(int channelId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long,MonopolyMarketCrossGroup> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketCrossGroupSql);
            int i = 1;
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                MonopolyMarketCrossGroup item = parse(rs);
                map.put(item.getGroupId(),item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateMonopolyMarketCrossGroupSql = "update `t_c_monopolymarket_cross_group` set serverList=?,state=?,fortressState=?,officeRefreshTime=?,kingResGrabRefreshTime=?,queenResGrabRefreshTime=?,fortressRefreshTime=?,changTime=?,officeCfgChangTime=?,kingCfgChangTime=?,queenCfgChangTime=? where channelId=? and groupId=?";
    @Override
    protected boolean updateData(MonopolyMarketCrossGroup monopolyMarketCrossGroup) {
        monopolyMarketCrossGroup.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketCrossGroup.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getFortressState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getOfficeRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getKingResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getQueenResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getFortressRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getOfficeCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getKingCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getQueenCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketCrossGroup.getGroupId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketCrossGroupSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketCrossGroupSql = "insert into `t_c_monopolymarket_cross_group`(channelId,groupId,serverList,state,fortressState,officeRefreshTime,kingResGrabRefreshTime,queenResGrabRefreshTime,fortressRefreshTime,changTime,officeCfgChangTime,kingCfgChangTime,queenCfgChangTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketCrossGroup monopolyMarketCrossGroup) {
        monopolyMarketCrossGroup.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketCrossGroup.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketCrossGroup.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getFortressState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getOfficeRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getKingResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getQueenResGrabRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getFortressRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getOfficeCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getKingCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketCrossGroup.getQueenCfgChangTime()));
        if (getDb().executeNoneQuery(insertMonopolyMarketCrossGroupSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketCrossGroup parseData(ResultSet rs) throws SQLException {
        MonopolyMarketCrossGroup monopolyMarketCrossGroup = new MonopolyMarketCrossGroup();
        monopolyMarketCrossGroup.setChannelId(rs.getInt("channelId"));
        monopolyMarketCrossGroup.setGroupId(rs.getLong("groupId"));
        monopolyMarketCrossGroup.setServerList(JSON.parseObject(rs.getString("serverList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        monopolyMarketCrossGroup.setState(rs.getInt("state"));
        monopolyMarketCrossGroup.setFortressState(rs.getInt("fortressState"));
        monopolyMarketCrossGroup.setOfficeRefreshTime(rs.getInt("officeRefreshTime"));
        monopolyMarketCrossGroup.setKingResGrabRefreshTime(rs.getInt("kingResGrabRefreshTime"));
        monopolyMarketCrossGroup.setQueenResGrabRefreshTime(rs.getInt("queenResGrabRefreshTime"));
        monopolyMarketCrossGroup.setFortressRefreshTime(rs.getInt("fortressRefreshTime"));
        monopolyMarketCrossGroup.setChangTime(rs.getInt("changTime"));
        monopolyMarketCrossGroup.setOfficeCfgChangTime(rs.getInt("officeCfgChangTime"));
        monopolyMarketCrossGroup.setKingCfgChangTime(rs.getInt("kingCfgChangTime"));
        monopolyMarketCrossGroup.setQueenCfgChangTime(rs.getInt("queenCfgChangTime"));

        return monopolyMarketCrossGroup;
    }
}
